package com.sumainfor.wx.system.aspect;

import com.sumainfor.common.Constants;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.wx.system.config.DataSourceConst;
import com.sumainfor.wx.system.config.DataSourceContextHolder;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-06-14 17:36
 * 弃用
 */
//@Aspect
//@Component
@Log4j2
@Deprecated
public class GenerateUUIDAspect implements Ordered {


    /**
     * 切入点
     */
    @Pointcut("execution(* com.sumainfor.common.utlis.ComUtils.getLocalTrmSeqNumByLook(..))")
    public void dataSourcePointCut() {

    }

    @Before(value = "dataSourcePointCut()")
    public void before(JoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        String dataBase = (String) args[1];
        switch (dataBase) {
            case Constants.DATA_SOURCE_TCEPC:
                DataSourceContextHolder.push(DataSourceConst.DATA_EPC);
                break;
            case Constants.DATA_SOURCE_GMIO:
                DataSourceContextHolder.push(DataSourceConst.DATA_GMIO);
                break;
            case Constants.DATA_SOURCE_TCMKT:
                DataSourceContextHolder.push(DataSourceConst.DATA_MARKET);
                break;
            case Constants.DATA_SOURCE_TCTEN:
                DataSourceContextHolder.push(DataSourceConst.DATA_TENANT);
                break;
            default:
                throw new TCException("不存在的数据库配置名称");
        }
    }

    @Override
    public int getOrder() {
        return -200;
    }

    @After(value = "dataSourcePointCut()")
    public void afterSwitchDS(JoinPoint point) {
        DataSourceContextHolder.poll();
    }
}
